package org.jeecg.modules.zhima.portal.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.zhima.admin.entity.ZmGame;
import org.jeecg.modules.zhima.admin.entity.ZmGamePlayer;
import org.jeecg.modules.zhima.admin.service.IZmGameService;
import org.jeecg.modules.zhima.portal.domain.Game;
import org.jeecg.modules.zhima.portal.domain.GamePlayer;
import org.jeecg.modules.zhima.portal.enums.GameStatus;
import org.jeecg.modules.zhima.portal.service.IGamePlayerService;
import org.jeecg.modules.zhima.portal.service.IGameService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;

/**
 * @Description: 赛事
 * @Author: jeecg-boot
 * @Date: 2021-02-21
 * @Version: V1.0
 */
@Api(tags = "赛事")
@RestController
@RequestMapping("/game")
@Slf4j
public class MZmGameController extends JeecgController<ZmGame, IZmGameService> {

    @Autowired
    private IGameService gameService;

    @Autowired
    private IZmGameService zmGameService;

    @Autowired
    private IGamePlayerService gamePlayerService;


    /*---------------------------------主表处理-begin-------------------------------------*/
    /**
     * 分页列表查询
     *
     * @param zmGame
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "赛事-分页列表查询")
    @ApiOperation(value = "赛事-分页列表查询", notes = "赛事-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(ZmGame zmGame,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        if (ObjectUtils.isNotNull(zmGame.getType()) && zmGame.getType() == 0){
            zmGame.setType(null);
        }
        QueryWrapper<ZmGame> queryWrapper = QueryGenerator.initQueryWrapper(zmGame, req.getParameterMap());
        queryWrapper.orderByDesc("create_time");
        if (ObjectUtils.isEmpty(zmGame.getStatus())){
            queryWrapper.gt("status", GameStatus.WAIT_CHECK.CODE);
        }
        Page<ZmGame> page = new Page<ZmGame>(pageNo, pageSize);
        IPage<Game> pageList = gameService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param game
     * @return
     */
    @AutoLog(value = "赛事-添加")
    @ApiOperation(value = "赛事-添加", notes = "赛事-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody Game game) {
        return Result.OK(gameService.publish(game));
    }

    /**
     * 详情查询
     *
     * @param zmGame
     * @return
     */
    @AutoLog(value = "赛事-详情查询")
    @ApiOperation(value = "赛事-详情查询", notes = "赛事-详情查询")
    @GetMapping(value = "/detail")
    public Result<?> detail(ZmGame zmGame) {
        Game game = gameService.getById(zmGame.getId());
        return Result.OK(game);
    }

    /**
     *   赛事报名
     *
     * @param game
     * @return
     */
    @AutoLog(value = "zm_player-赛事报名")
    @ApiOperation(value="zm_player-赛事报名", notes="zm_player-赛事报名")
    @PostMapping(value = "/join")
    public Result<?> join(@RequestBody Game game) {
        return Result.OK(gameService.join(game));
    }

    /**
     *   赛事取消报名editPlayerScore
     *
     * @param game
     * @return
     */
    @AutoLog(value = "zm_player-赛事取消报名")
    @ApiOperation(value="zm_player-赛事取消报名", notes="zm_player-赛事取消报名")
    @PostMapping(value = "/unJoin")
    public Result<?> unJoin(@RequestBody Game game) {
        return Result.OK(gameService.unJoin(game.getId()));
    }

    /**
     *   我参与的赛事
     *
     * @param game
     * @return
     */
    @AutoLog(value = "zm_player-我参与的赛事")
    @ApiOperation(value="zm_player-我参与的赛事", notes="zm_player-我参与的赛事")
    @PostMapping(value = "/myJoin")
    public Result<?> myJoin(@RequestBody Game game) {
        return Result.OK(gameService.listMyJoin(game));
    }

    /**
     *   我发布的赛事
     *
     * @param game
     * @return
     */
    @AutoLog(value = "zm_player-我发布的赛事")
    @ApiOperation(value="zm_player-我发布的赛事", notes="zm_player-我发布的赛事")
    @PostMapping(value = "/myPublish")
    public Result<?> myPublish(@RequestBody Game game) {
        return Result.OK(gameService.myPublish(game));
    }

    /**
     *   赛事结算
     *
     * @param game
     * @return
     */
    @AutoLog(value = "zm_player-赛事结算")
    @ApiOperation(value="zm_player-赛事结算", notes="zm_player-赛事结算")
    @PostMapping(value = "/finish")
    public Result<?> finish(@RequestBody Game game) {
        return Result.OK(gameService.finish(game));
    }

    /**
     *   取消赛事
     *
     * @param game
     * @return
     */
    @AutoLog(value = "game-取消赛事")
    @ApiOperation(value="game-取消赛事", notes="game-取消赛事")
    @PostMapping(value = "/cancel")
    public Result<?> cancel(@RequestBody Game game) {
        return Result.OK(gameService.cancel(game));
    }

    /**
     * 编辑
     *
     * @param zmGame
     * @return
     */
    @AutoLog(value = "赛事-编辑")
    @ApiOperation(value = "赛事-编辑", notes = "赛事-编辑")
    @PostMapping(value = "/edit")
    public Result<?> edit(@RequestBody ZmGame zmGame) {
        gameService.edit(zmGame);
        return Result.OK("编辑成功!");
    }

    /**
     * 编辑选手成绩
     *
     * @param gamePlayer
     * @return
     */
    @AutoLog(value = "赛事-编辑选手成绩")
    @ApiOperation(value = "赛事-编辑选手成绩", notes = "赛事-编辑选手成绩")
    @PostMapping(value = "/editPlayerScore")
    public Result<?> editPlayerScore(@RequestBody GamePlayer gamePlayer) {
        return Result.OK(gamePlayerService.editPlayerScore(gamePlayer));
    }

    /**
     * 延期
     *
     * @param zmGame
     * @return
     */
    @AutoLog(value = "赛事-延期")
    @ApiOperation(value = "赛事-延期", notes = "赛事-延期")
    @PostMapping(value = "/delay")
    public Result<?> delay(@RequestBody ZmGame zmGame) {
        gameService.delay(zmGame);
        return Result.OK("编辑成功!");
    }

    /**
     * 计算
     * @return
     */
    @AutoLog(value = "赛事-计算")
    @ApiOperation(value = "赛事-计算", notes = "赛事-计算")
    @GetMapping(value = "/calculate")
    public Result<?> calculate() {
        gameService.calculate();
        return Result.OK();
    }


    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "赛事-通过id删除")
    @ApiOperation(value = "赛事-通过id删除", notes = "赛事-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        zmGameService.delMain(id);
        return Result.OK("删除成功!");
    }

}
